package adventure.commands;

import adventure.*;
import adventure.placeable.Fightable;
import player.*;
import adventure.placeable.Mobile;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Consider extends AbstractCommand {

    public Consider(Adventure a) {
        super(a, new String[]{"CONSIDER"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 1) {
            return "Consider your chances against who?";
        } else if (((AdventureEdit) adv).getRoomManager().isLightPresent(p.x(), p.y(), p.x())) {
            return consider(p, Util.consolidateArguments(args));
        } else {
            return "You cannot see anything in the darkness!";
        }
    }

    private String consider(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        Mobile mob = rooms.getRoomMobile(x, y, z, target);

        if (mob.getID() > 0) {
            out.broadcastLocalToPlayer(player,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " considers their chances against "
                    + ColourUtil.colourise(mob.getName(), ColourUtil.red) + "!");
            return player.consider((Fightable) mob);
        } else {
            return considerPlayer(player, target);
        }
    }

    private String considerPlayer(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();
        int targetPlayer = rooms.getRoomPlayer(x, y, z, target);

        if (targetPlayer > 0 && player.getID() != targetPlayer) {
            Player[] excPlayers = new Player[2];
            excPlayers[0] = player;
            excPlayers[1] = players.getOnlinePlayer(targetPlayer);

            out.broadcastLocalToPlayers(excPlayers,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " considers their chances against "
                    + ColourUtil.colourise(players.getOnlinePlayer(targetPlayer).getName(), ColourUtil.magenta)
                    + "!");

            out.broadcastLocalToPlayer(player,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " considers their chances against YOU!");

            return player.consider((Fightable) players.getOnlinePlayer(targetPlayer));
        } else if (targetPlayer == player.getID()) {
            out.broadcastLocalToPlayer(player,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " considers their chances against "
                    + "theirself...");
            return "You consider your chances against yourself.. It's an even match.";
        } else {
            return "You don't see any " + target + " here!";
        }
    }
}
